{***************************************************************
    SpyGestion  Copyright (C) 2008 Christophe KOALI <nature2gas@gmail.com>
    
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
    
****************************************************************}
unit frmProfilChoix;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, hhCheckBox, StdCtrls, Menus, BarMenus, sFonctionsChaines, modConstantes;

type
  TformProfilChoix = class(TForm)
    lstProfilsList: TListBox;
    chkLoadAutoProfil: ThhCheckBox;
    Label1: TLabel;
    BcBarMainMenu1: TBcBarMainMenu;
    Fichier1: TMenuItem;
    Nouveauprofil1: TMenuItem;
    N1: TMenuItem;
    menQuitter1: TMenuItem;
    cmdProfilLoad: TButton;
    cmdAnnuler: TButton;
    procedure chkLoadAutoProfilClick(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure menQuitter1Click(Sender: TObject);
    procedure cmdProfilLoadClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Nouveauprofil1Click(Sender: TObject);
  private
    { Private declarations }

    procedure ClearSelection;

  public
    { Public declarations }
    ProfilToLoad: string;

    procedure PrepareToShow(AllowCancel: boolean);
    procedure RefreshProfilList;

    function DoNewProfil: boolean;


  end;

var
  formProfilChoix: TformProfilChoix;

implementation

uses uProfilCreation,
     gnugettext,
     modLocalSettings;

{$R *.dfm}


function TformProfilChoix.DoNewProfil: boolean;
var i: integer;
begin

    if frmProfilCreation = nil then begin
       // on créé le profil
       frmProfilCreation := TfrmProfilCreation.Create(Self);
    end;

    frmProfilCreation.ClearForm;

    // Affiche la form
    Result := frmProfilCreation.ShowModal = mrOk;

    if Result then begin
       // un profil a été créé

       RefreshProfilList;

       ClearSelection;

       i := lstProfilsList.Items.IndexOf(frmProfilCreation.NewProfilFile);

       if NoT(i < 0) then begin
          // le selectionne
          lstProfilsList.Selected[i] := true;
       end;

    end else begin
       // aucun profil n'a été créé

    end;

end;


procedure TformProfilChoix.RefreshProfilList;
var i: integer;
    c: longword;
    TokenFiles, CurrFile, FullPath: string;
begin

   lstProfilsList.Items.Clear;


   if DirectoryExists(Global_DossierProfils) = false then begin
      // il existe deja normalement
      exit;
   end;

   TokenFiles := sfcFileEnumFichiersDir(Global_DossierProfils, '*.ini', '%s', '|', c);

   if c = 0 then exit;

   for i := 1 to c do begin
      CurrFile := sfcTokenGet(TokenFiles, i, '|');
      FullPath := Global_DossierProfils + CurrFile;

      if FileExists(FullPath) then begin
         lstProfilsList.Items.Add(CurrFile);

      end;

   end;


end;


procedure TformProfilChoix.chkLoadAutoProfilClick(Sender: TObject);
begin
    Global_LocalSetting.ConfLoadTheLastestProfilAtStarup := IIF(chkLoadAutoProfil.Checked, 1, 0);
end;

procedure TformProfilChoix.ClearSelection;
var i: integer;
begin
    if lstProfilsList.Items.Count > 0 then begin
       for i := 0 to lstProfilsList.Items.Count - 1 do begin
          lstProfilsList.Selected[i] := false;
       end;
    end;


end;


procedure TformProfilChoix.PrepareToShow(AllowCancel: boolean);
var i: integer;
begin
    ProfilToLoad := '';

    RefreshProfilList;

    cmdAnnuler.Visible := AllowCancel;

    if AllowCancel then begin
       menQuitter1.Caption := _('Fermer');
    end;


    if lstProfilsList.Items.Count > 1 then begin
       ClearSelection;

       i := lstProfilsList.Items.IndexOf(Global_LocalSetting.IniLastProfilLoaded);

       if i >= 0 then begin
          lstProfilsList.Selected[i] := true;
       end;

    end else if lstProfilsList.Items.Count = 1 then begin
       // un seul élément, il doit être selectionné
       lstProfilsList.Selected[0] := true;

    end else begin
       // aucun profil
       DoNewProfil;
    end;

end;

procedure TformProfilChoix.menQuitter1Click(Sender: TObject);
begin
    if cmdAnnuler.Visible then begin
       Close;
       ModalResult := mrCancel;
    end else begin
       Application.Terminate;
    end;

end;

procedure TformProfilChoix.cmdProfilLoadClick(Sender: TObject);
var i: integer;
begin

    if lstProfilsList.Count = 0 then exit;

    cmdProfilLoad.Enabled := false;

    (*
    // sauvegarde l'ancien profil
    if FCurrentProfil.IsValidProfil then begin
       FCurrentProfil.SaveToFile('');

       if FCurrentPlayer.allPlanetes.Count > 0 then begin
          FCurrentPlayer.SaveToFile('');
       end;

       Status(format(_('Ancien profil %s sauvegardé.'), [FCurrentProfil.Pseudo]));
    end;
    *)

    ProfilToLoad := '';

    for i := 0 to lstProfilsList.Count - 1 do begin
        if lstProfilsList.Selected[i] then begin
           ProfilToLoad := lstProfilsList.Items.Strings[i];
           break;
        end;

    end;

    if ProfilToLoad = '' then begin
       cmdProfilLoad.Enabled := true;
       exit;
    end;

    Global_LocalSetting.IniLastProfilLoaded := ProfilToLoad;
    Global_LocalSetting.SaveToFile('', [lsoIniData]);

    ProfilToLoad := Global_DossierProfils + ProfilToLoad;

    (*
    timServiceDelayCmd.Interval := 1500;
    LoadProfilFullPath(Fichier);
    *)

    cmdProfilLoad.Enabled := true;

    Close;
    ModalResult := mrOK;
    
end;

procedure TformProfilChoix.FormCreate(Sender: TObject);
begin
   chkLoadAutoProfil.Checked := Global_LocalSetting.ConfLoadTheLastestProfilAtStarup <> 0;

   // on initialise rien d'autre car tout est géré par PrepareToShow

   
    // traduction, car la form est créé sur le vol
    TranslateComponent(Self);
end;

procedure TformProfilChoix.FormDestroy(Sender: TObject);
begin
   if frmProfilCreation <> nil then begin
      frmProfilCreation.Free;
   end;
end;

procedure TformProfilChoix.Nouveauprofil1Click(Sender: TObject);
begin
    DoNewProfil;
end;

end.
